package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/role")
@RestController
public class RoleController {

    @Reference
    private RoleService roleService;

    @RequestMapping("/findroleByUserId")
    //根据用户id查询绑定的角色
    public Result findrolesByUserId(@RequestBody User user) {
        Integer id = user.getId();
        System.out.println(id);

        try {
            List<Integer> list = roleService.findroleByUserId(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }

    }


    @RequestMapping("/findAll")
    //查询所有角色清单
    public Result findAll() {
        try {
            List<Role> all = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, all);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }

    }
}
